import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from numpy.ma.core import inner
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import learning_curve, validation_curve, GridSearchCV,train_test_split
data = pd.read_csv('D:\PythonProject\考核\data1.csv')
ohe = OneHotEncoder()
data_pre = {
    'house': [1],
    'car': [0],
    'appearance': [6],
    'family_status': [7],
    'parents_status': [8],
    'lifestyle': [6],
    'education': [4],
    'personality': [7],
    'interests': [5],
    'success':[-1]
}
data_pre = pd.DataFrame(data_pre)
data = pd.concat([data_pre,data], axis=0)
def change(c_data,name):
    global ohe
    new = ohe.fit_transform(c_data[[name]]).toarray().astype(int)
    c_data.drop([name], axis=1, inplace=True)
    c_data[ohe.get_feature_names_out([name])] = new

change(data,'house')
change(data,'car')
change(data,'education')
y = data['success'].values
y_pre = y[1]
y = y[1:]
X = data.drop(columns='success').values
X_pre = X[1,np.newaxis]
X = X[1:]
clf = RandomForestClassifier(n_estimators=100)
range_size = np.array([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9])
sizes,train,test=learning_curve(clf, X, y,train_sizes=range_size,n_jobs=1,cv=5,scoring='accuracy')
train = np.mean(train,axis=1)
test = np.mean(test,axis=1)
plt.plot(sizes,train,label='train',color='r')
plt.plot(sizes,test,label='test',color='b')
plt.show()
index = test.argmax()
max_size = 1.0-range_size[index]
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=max_size,random_state=42)
clf.fit(X_train,y_train)
print(clf.predict(X_pre))









